//
//  SYVisitorView.swift
//  SYWeibo
//
//  Created by Eric on 16/7/19.
//  Copyright © 2016年 Eric. All rights reserved.
//

import UIKit
import SnapKit
class SYVisitorView: UIView {

    //声明一个闭包
    var closure:(() -> ())?
    //初始化控件
    override init(frame: CGRect) {
        super.init(frame:frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //监听方法
    @objc private func loginButtonClick(){
        closure?()
    }
    
    //MARK: -  供外界使用

    func setupVisitorViewInfo(imgName: String?, title:String?){
        
        //判断imgName 和 title 是否 为nil
        //如果不是nil 代表设置的为 消息  返现  我
        if let img = imgName, let tit = title{
        messageLabel.text = tit
        iconImageView.image = UIImage(named: img)
        feedImageView.hidden = true
        }else{
        //代表首页
            setupAnim()
        
        }
        
    }
    //设置圆圈动画
    func setupAnim(){
    let anim = CABasicAnimation(keyPath: "transform.rotation")
        //设置 tovalue
        anim.toValue = M_PI * 2
        //设置时间
        anim.duration  = 10
        //设置重复次数
        anim.repeatCount = MAXFLOAT
        //当切换控制器或者程序退出到后台的   默认动画会停止
        anim.removedOnCompletion = false
        //添加动画
        feedImageView.layer.addAnimation(anim, forKey: nil)
    
    }
    
    //MARK: -设置视图
    func setupUI(){
    
        backgroundColor = RGB(237, green: 237, blue: 237)
    // 01 添加控件
        addSubview(feedImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(loginButton)
        addSubview(regButton)
    // 02 设置约束
       //01
        feedImageView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self)
        }
        
        //02
        maskImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
            
        }
        //03
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
            
        }
        //04
        messageLabel.snp_makeConstraints { (make) -> Void in
           
            make.size.equalTo(CGSize(width: 230, height: 36))
            make.top.equalTo(maskImageView.snp_bottom).offset(16)
            make.centerX.equalTo(self)
        }
        
        //05
    loginButton.snp_makeConstraints { (make) -> Void in
        make.size.equalTo(CGSize(width: 100, height: 36))
        make.top.equalTo(messageLabel.snp_bottom).offset(16)
        make.leading.equalTo(messageLabel)
        }
    //06
        regButton.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(loginButton)
            make.top.equalTo(loginButton)
            make.trailing.equalTo(messageLabel)
            
        }
    
    }

    
    //MARK: - 懒加载控件
    //圆圈
    private  lazy var feedImageView: UIImageView = UIImageView(imgName: "visitordiscover_feed_image_smallicon")
    //遮挡
    private lazy var maskImageView: UIImageView = UIImageView(imgName: "visitordiscover_feed_mask_smallicon")
    //房子
    private lazy var iconImageView: UIImageView = UIImageView(imgName: "visitordiscover_feed_image_house")
    //文字
    private lazy var messageLabel: UILabel = {
        let lab = UILabel(textColor: UIColor.grayColor(), textFont: 14, maxWidth: 0)
        lab.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        lab.numberOfLines = 0
        lab.textAlignment = .Center
        return lab
    }()
    
    //登陆
    private lazy var loginButton: UIButton = {
    let button = UIButton()
        button.addTarget(self, action: "loginButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        button.setTitle("登录", forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        button.setTitleColor(ThemeColor, forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        return button
    }()
    //注册
    private lazy var regButton: UIButton = {
    let button = UIButton()
        //添加点击时间
        button.addTarget(self, action: "loginButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(ThemeColor, forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        return button
        
    }()

}

